import { createRouter, createWebHistory } from 'vue-router'
import LoginView from '../views/LoginView.vue'
import NotFound from '../views/Not.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: 'login'
    },
    { path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound },
    {
      path: '/login',
      name: 'login',
      component: LoginView
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('../views/HomeView.vue'),
      children: [
        {
          path: '/user/list',
          name: 'userlist',
          component: () => import('../views/user/list.vue'),
        },
        {
          path: '/user/add',
          name: 'useradd',
          component: () => import('../views/user/add.vue'),
        },
        {
          path: '/user/del',
          name: 'userdel',
          component: () => import('../views/user/del.vue'),
        },
        {
          path: '/user/edit',
          name: 'useredit',
          component: () => import('../views/user/edit.vue'),
        },
        {
          path: '/shop/list',
          name: 'shoplist',
          component: () => import('../views/shop/list.vue'),
        },
        {
          path: '/shop/add',
          name: 'shopadd',
          component: () => import('../views/shop/add.vue'),
        },
        {
          path: '/shop/del',
          name: 'shopdel',
          component: () => import('../views/shop/del.vue'),
        },
        {
          path: '/shop/edit',
          name: 'shopedit',
          component: () => import('../views/shop/edit.vue'),
        },
        {
          path: '/role/list',
          name: 'rolelist',
          component: () => import('../views/role/list.vue'),
        },
        {
          path: '/role/add',
          name: 'roleadd',
          component: () => import('../views/role/add.vue'),
        },
        {
          path: '/role/del',
          name: 'roledel',
          component: () => import('../views/role/del.vue'),
        },
        {
          path: '/role/edit/:id',
          name: 'roleedit',
          component: () => import('../views/role/edit.vue'),
        },
        {
          path: '/auth/list',
          name: 'authlist',
          component: () => import('../views/auth/list.vue'),
        }
      ]
    }
  ]
})


router.beforeEach((to, from) => {
  let token=localStorage.getItem('token')
  if(to.path!='/login'){
    if(!token){
      return {
        path:'/login'
      }
    }
  }
})

export default router
